import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.util.Hashtable;
import java.util.Enumeration;
import java.util.LinkedList;

public class RunCET {
	
	private Hashtable ht;

	public RunCET(){
		
		try{
		
			Hashtable<String,LinkedList<String>> htable = new Hashtable <String, LinkedList<String>>();
			
			BufferedReader names =  new BufferedReader(new InputStreamReader(new FileInputStream ("names.txt"), "UTF-8"));
			
			//evalutation data
			BufferedReader evalA =  new BufferedReader(new InputStreamReader(new FileInputStream ("evaluationA.txt"), "UTF-8"));
			BufferedReader evalB =  new BufferedReader(new InputStreamReader(new FileInputStream ("evaluationB.txt"), "UTF-8"));
			//result
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter (new FileOutputStream("result.txt"),"UTF-8"));
		    
////////////dealing with emotion
			htable = new Hashtable <String, LinkedList<String>>();
			
			String n="";
			int totalStudents=0;
			while((n = names.readLine())!= null)
			{
				String line="";
				int index=0;
				String targetName = n;
				boolean turnon = false;
				BufferedReader emotion =  new BufferedReader(new InputStreamReader(new FileInputStream ("emo.txt"), "UTF-8"));
				while((line = emotion.readLine()) != null)
				{
					String[] words = line.split("\t");
					
					//exclude the first line which represent the name of columns
					if(index > 0)
					{
						int wordIndex=0;
						for(String s : words)
						{
							if(wordIndex==0)
							{
								if(s.toUpperCase().compareTo(targetName.toUpperCase())!=0)
								{
									break;
								}
								else if(!isInHashTable(s,htable))
								{
									LinkedList<String> one = new LinkedList<String>();
									htable.put(targetName, one);
									totalStudents++;
									turnon = true;
								}
							}
							else
							{
								if(turnon)
								{
									LinkedList<String> existedone = (LinkedList<String>)htable.get(targetName);
									existedone.add(s);
									htable.put(targetName,existedone);
								}
							}
							
							wordIndex++;
						}
					}
					
					index++;
				}
				
				emotion.close();
			}
			
			names.close();
			//System.out.println("total:"+totalStudents);
			
			printActivityEmotion(5,htable);
			
			/*//printout each name and its corresponding information
			
			LinkedList<String> target = (LinkedList<String>)htable.get(targetName);
			System.out.println("*"+targetName+"*");
			int turnMax = 10;
			int i=0;
			while(i < target.size())
			{
				if(i !=0 && i % turnMax == 0)
					System.out.print("\n");
				System.out.print(target.get(i)+"\t");
				i++;
			}*/
			
			/*
			String line="", prefix="act";
			//for(int i=1;i<=5;i++)
			//{
				prefix = prefix + gIndex;
				BufferedReader activity =  new BufferedReader(new InputStreamReader(new FileInputStream (prefix+".txt"), "UTF-8"));
				
				line="";
				int index=0;
				while((line = activity.readLine()) != null)
				{
					String[] words = line.split("\t");
					
					//exclude the first line which represent the name of columns
					if(index > 0)
					{
						int wordIndex=0;
						String name=null;
						for(String s : words)
						{
							if(wordIndex==0)
							{
								if(!isInHashTable(s,htable))
								{
									LinkedList<String> one = new LinkedList<String>();
									htable.put(s, one);
								}
								name = s;
							}
							else
							{
								LinkedList<String> existedone = (LinkedList<String>)htable.get(name);
								existedone.add(s);
							}
							
							wordIndex++;
						}
					}
						
					index++;
				}
				
				processHashtable(htable,gIndex);
				activity.close();
			//}
			*/	
			
			System.out.println("\nDone!");
			////////////end of dealing with emotion
			
		}catch (Exception e){
			e.printStackTrace();
		}
		
	}
	
	void printActivityEmotion(int i,  Hashtable<String,LinkedList<String>> ht)
	{
		//int total = ht.size();
		//System.out.println("hash table size:"+ total);

		Enumeration<String> e = ht.keys();
		while(e.hasMoreElements())
		{
			String name = (String)e.nextElement();
			//System.out.println(name);
			
			LinkedList<String> list = ht.get(name);
			String emotions = getEmotion(i,list);
			System.out.println(name+"\t"+emotions);
		}
		
	}
	
	String getEmotion(int i, LinkedList<String> list)
	{
		String ret = "";
		int j=0;
		
		//0:initial, 1: fluency, 2: flexibility, 3: originality, 4: elaboration, 5: problem sensitivity
		switch (i){
		case 0: 
				while(j < 10)
				{
					ret = ret + "\t" + list.get(j);
					
					j++;
				}
				
			break;
		case 1: 
				int fixnum = i;
				boolean starton = false, endon = false;
				int totalTurn =0;
				while(j < list.size())
				{
					if(j>9 && j%10==0)
					{
						if(Integer.parseInt(list.get(j))==1)
						{
							starton=true;
							if(j < 40)
								ret = "";
						}
						else
							starton=false;
					}
					
					if(j < 40 && starton == true)
						ret = ret + "\t" + list.get(j);
					
					j++;
				}
			break;
			case 2: 
				starton = false;
				while(j < list.size())
				{
					if(j>9 && j%10==0)
					{
						if(Integer.parseInt(list.get(j))==2)
						{
							starton=true;
							ret="";
						}
						else
							starton=false;
					}
					
					if(starton == true)
						ret = ret + "\t" + list.get(j);
					
					j++;
				}
					
			break;
		case 3: 
			starton = false;
			while(j < list.size())
			{
				if(j>9 && j%10==0)
				{
					if(Integer.parseInt(list.get(j))==3)
					{
						starton=true;
						ret="";
					}
					else
						starton=false;
				}
				
				if(starton == true)
					ret = ret + "\t" + list.get(j);
				
				j++;
			}
			break;
		case 4: 
			starton = false;
			while(j < list.size())
			{
				if(j>9 && j%10==0)
				{
					if(Integer.parseInt(list.get(j))==4)
					{
						starton=true;
						ret="";
					}
					else
						starton=false;
				}
				
				if(starton == true)
					ret = ret + "\t" + list.get(j);
				
				j++;
			}
			break;
		case 5: 
			starton = false;
			while(j < list.size())
			{
				if(j>9 && j%10==0)
				{
					if(Integer.parseInt(list.get(j))==5)
					{
						starton=true;
						ret="";
					}
					else
						starton=false;
				}
				
				if(starton == true)
					ret = ret + "\t" + list.get(j);
				
				j++;
			}
			break;
		default: 
			break;
		}
		
		return ret;
	}
	
	void storePersonEmotion(String name, Hashtable<String,LinkedList<String>> ht)
	{
		
		
	}
	
	void processHashtable(Hashtable<String,LinkedList<String>> ht, int index)
	{
		
		
	}
	
	boolean isInHashTable(String key, Hashtable<String, LinkedList<String>> ht)
	{
			Enumeration<String> e = ht.keys();
	       
	        while(e.hasMoreElements()){        

	        	if(e.nextElement().toString().compareTo(key)==0)//equal
	        		return true;
	        	
	        }
		
		return false;
	}
	
	public static void main(String args[]){
		
		new RunCET();
	}

}
